﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using Utilities.Common;
using System.Collections;

namespace Utilities.UI
{
    public static class DropDownListExtensions
    {
        public static void Fill<U, T>(this DropDownList ddl, U datasource, string textField) where U : List<T>
        {
            if (datasource.Count != 0)
            {
                ClassInfo info = ClassInfoCache.GetClassInfo(datasource[0]);
                ddl.DataSource = datasource;
                ddl.DataValueField = info.Properties.PropId.Name;
                if(textField!="") ddl.DataTextField = textField;
                ddl.DataBind();
               // ddl.SelectedValue = "0";
            }
            else
            {
#warning ver
            }

        }
        public static void Fill<U, T>(this DropDownList ddl, U datasource)where U:List<T>
        {
            Fill<U, T>(ddl, datasource, "");
        }

        public static void Fill(this DropDownList ddl, object datasource, string textField)
        {
            IList data;
            if (datasource is System.Collections.IList)
            {
                data = datasource as IList;
                if (data.Count!=0)
                {
                    ClassInfo info = ClassInfoCache.GetClassInfo(data[0]);
                    ddl.DataSource = datasource;
                    ddl.DataValueField = info.Properties.PropId.Name;
                    if (textField != "") ddl.DataTextField = textField;
                    ddl.DataBind();
                }

            }
        }


    }
}
